//
//  LLHomeTableViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/2.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLHomeTableViewController.h"
#import "LLStatusCell.h"
#import "LLStatusLayout.h"
#import "LLStatusModel.h"
#import "LLTestViewController.h"

#import "LLNewsImageDetialVC.h"
#import "LLNewsVideoDetialVC.h"
#import "QWebVC.h"

#import "videocell.h"
#import "oneImageCell.h"
#import "threeImageCell.h"
#import "titleTableViewCell.h"
#import "LLAd_cell.h"

@interface LLHomeTableViewController ()

@end

@implementation LLHomeTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
       [self getNewsData];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page= 1;
        [self.models removeAllObjects];
        [self getNewsData];
    }];
    [self.tableView.mj_header beginRefreshing];
   
}

- (void)configTableView
{
    [super configTableView];
    self.tableView.height = LLScreenHeight-self.navH-50-self.tabH;
    [self.tableView registerCellByClassName:@"LLStatusCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 14, 0, 14);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    LLStatusModel *model = self.models[indexPath.row];
    if ([model.type intValue] == 1) {

        return 290;
    }
    else if ([model.type intValue] == 2) {

        return 120;
    }
    else if ([model.type intValue] == 3) {

        return 190;
    }
    else if ([model.type intValue] == 4) {

        return 120;
    }
    else if ([model.type intValue] == 1000) {

        return 290;
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    LLStatusModel *model = self.models[indexPath.row];
    if ([model.type intValue] == 1) {

        static NSString *string = @"videocell";
        videocell *cell = [[videocell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }
    else if ([model.type intValue] == 2) {

        static NSString *string = @"oneImageCell";
        oneImageCell *cell = [[oneImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }
    else if ([model.type intValue] == 3) {

        static NSString *string = @"threeImageCell";
        threeImageCell *cell = [[threeImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }
    else if ([model.type intValue] == 4) {

        static NSString *string = @"titleTableViewCell";
        titleTableViewCell *cell = [[titleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }
    else if ([model.type intValue] == 1000) {

        static NSString *string = @"LLAd_cell";
        LLAd_cell *cell = [[LLAd_cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }

    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    LLStatusModel *model = self.models[indexPath.row];
    if ([model.type intValue] == 1) {

        LLNewsVideoDetialVC *vc = [LLNewsVideoDetialVC new];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([model.type intValue] == 2 ||
             [model.type intValue] == 3 ||
             [model.type intValue] == 4) {

        LLNewsImageDetialVC *vc = [LLNewsImageDetialVC new];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([model.type intValue] == 1000) {

        QWebVC *vc = [QWebVC new];
        vc.urlString = model.url;
        vc.showNavBar = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void) setSearch_text:(NSString *)search_text
{
    _search_text = search_text;
    self.page = 1;
    [self getNewsData];
}

//获取新闻数据
-(void)getNewsData{
    NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"36",@"new_type":@(self.news_type),@"interest_id":self.interest_id,@"page":@(self.page)}];

    if (self.search_text.length > 0) {
        [dic setObject:self.search_text forKey:@"new_title"];
    }

    if (self.page == 1) {
        [self.models removeAllObjects];
        [self.tableView reloadData];
    }
    
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        self.page++;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([result[@"code"] integerValue] == 0) {

            NSArray *resultArr = [NSArray yy_modelArrayWithClass:[LLStatusModel class] json:result[@"data"]];

            NSArray *array = result[@"data"];
            for (NSInteger i = 0; i < array.count; i++) {

                NSDictionary *dict = array[i];
                LLStatusModel *model = LLStatusModel.new;

                if ([dict[@"is_poster"] intValue] == 1) {

                    model.img = [NSString stringWithFormat:@"%@",dict[@"img"]];
                    model.url = [NSString stringWithFormat:@"%@",dict[@"url"]];
                    model.title = [NSString stringWithFormat:@"%@",dict[@"title"]];
                    model.info = [NSString stringWithFormat:@"%@",dict[@"info"]];
                    model.type = @"1000";
                }
                else {

                    if ([dict[@"type"] intValue] == 1 ||
                        [dict[@"type"] intValue] == 2 ||
                        [dict[@"type"] intValue] == 3 ||
                        [dict[@"type"] intValue] == 4) {


                        model.comment_num = [NSString stringWithFormat:@"%@",dict[@"comment_num"]];
                        model.newsId = [NSString stringWithFormat:@"%@",dict[@"id"]];
                        model.img_url = dict[@"img_url"] ? dict[@"img_url"] : @[];
                        model.is_family = [NSString stringWithFormat:@"%@",dict[@"is_family"]];
                        model.look_num = [NSString stringWithFormat:@"%@",dict[@"look_num"]];
                        model.newsTitle = [NSString stringWithFormat:@"%@",dict[@"new_title"]];
                        model.u_name = [NSString stringWithFormat:@"%@",dict[@"u_name"]];
                        model.type = [NSString stringWithFormat:@"%@",dict[@"type"]];
                        model.video_img = [NSString stringWithFormat:@"%@",dict[@"video_img"]];
                        model.video_url = [NSString stringWithFormat:@"%@",dict[@"video_url"]];

                        model.time = [self compareCurrentTime:[self getDateFromString:dict[@"time"]]];
                    }
                }

                [self.models addObject:model];
            }

            if (resultArr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                return ;
            }
            
            [self.tableView reloadData];
        }else{
             [SVProgressHUD showErrorWithStatus:result[@"msg"]];
        }
        
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        
    }];
}
@end
